package cn.schoolwow.util.test.module.query.jsonarray.flow.query;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.jsonarray.QueryJSONArray;
import com.alibaba.fastjson.JSONArray;
import org.junit.Assert;

public class JSONArrayQueryOrderByFieldNameFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONArray userArray = flowContext.checkData("userArray", JSONArray.class);

        {
            JSONArray filterArray = QueryJSONArray.newQuery(userArray)
                    .orderByAsc("age")
                    .execute()
                    .getArray();
            Assert.assertEquals(filterArray.size(), filterArray.size());
            for(int i=1;i<filterArray.size();i++){
                int age = filterArray.getJSONObject(i).getIntValue("age");
                Assert.assertEquals(i, age);
            }
            Assert.assertFalse(filterArray.getJSONObject(0).containsKey("age"));
        }
        {
            JSONArray filterArray = QueryJSONArray.newQuery(userArray)
                    .orderByDesc("age")
                    .execute()
                    .getArray();
            Assert.assertEquals(filterArray.size(), filterArray.size());
            for(int i=0;i<filterArray.size()-1;i++){
                int age = filterArray.getJSONObject(i).getIntValue("age");
                Assert.assertEquals(filterArray.size()-1-i, age);
            }
            Assert.assertFalse(filterArray.getJSONObject(filterArray.size()-1).containsKey("age"));
        }
    }

    @Override
    public String name() {
        return "用户实例列表按照年龄排序";
    }
}
